﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.MemberUser
{
    public partial class ActivateAccount : System.Web.UI.Page
    {
        public string _uid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserKit.GetUserAccountWithoutException() != null)
            {
                Response.Redirect("/MemberUser/Default.aspx");
            }
            if(!IsPostBack)
            {
        hid_uid.Value= Request["uid"];
            }
            _uid = hid_uid.Value;

            ClientScriptManager csm = this.Page.ClientScript;

            #region 邮箱激活
            if (Request["type"] == "email")
            {
                string _checkString = Request["checkstring"];
               

                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    EmailCheckInfo _checkInfo = session.CreateCriteria<EmailCheckInfo>().Add(Expression.Eq("CheckString", _checkString)).Add(Expression.Eq("MemberAccount", _uid)).Add(Expression.Eq("Status", "0")).UniqueResult<EmailCheckInfo>();

                    if (_checkInfo == null)
                    {
                        csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('链接无效!');location.href='/MemberUser/Login.aspx'", true);
                    }
                    else
                    {
                        _checkInfo.Status = "1";

                        MemberInfo _member = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("AccountId", _checkInfo.MemberAccount)).UniqueResult<MemberInfo>();

                        _member.Status = "1";

                        session.Update(_checkInfo);
                        session.Update(_member);
                        session.Flush();

                        if (!UserKit.UserLoginMember(_member.Email, _member.Password))
                        {
                            csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('登录失败!');location.href='/MemberUser/Login.aspx'", true);
                        }
                        else
                        {
                            csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('激活成功!');location.href='/MemberUser/Default.aspx'", true);
                        }

                    }
                } 
            #endregion
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;

            try
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    MemberInfo member = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("AccountId", _uid)).Add(Expression.Eq("Status", "0")).UniqueResult<MemberInfo>();
                    if (member != null)
                    {
                        EmailCheckInfo _checkInfo = session.CreateCriteria<EmailCheckInfo>().Add(Expression.Eq("CheckString", tb_id.Text.Trim())).Add(Expression.Eq("MemberAccount", _uid)).Add(Expression.Eq("Status", "0")).UniqueResult<EmailCheckInfo>();
                        if (_checkInfo != null)//判断手机验证码是否正确
                        {
                            member.Status = "1";
                        }
                        else
                        {
                            csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('验证码无效!');", true);
                            return;
                        }
                        session.Update(member);
                        _checkInfo.Status = "1";
                        session.Update(_checkInfo);
                        session.Flush();
                        csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('注册成功!');location.href='/MemberUser/Default.aspx'", true);
                    }
                    else
                    {
                        csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('验证码无效!');", true);
                    }
                }
            }
            catch (System.Exception ex)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('请先退出其他登录账号，身份验证失败！');", true);
            }
        }
    }
}